using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using stsdev.Builders.SolutionFiles;
using stsdev.Builders.SourceFiles;


namespace stsdev
{

    class WebServiceProvider : ISolutionProvider
    {
        private ProjectFileBuilder _projectFileBuilder;

        public WebServiceProvider()
        {
            _projectFileBuilder = new ProjectFileBuilder();
        }

        public string Title
        {
            get { return "Web Service Solution (C# assembly)"; }
        }

        public void InitializeSolution()
        {
            SolutionBuilder.AssemblyDeployment = true;
            SolutionBuilder.SafeControlSettings = false;
            SolutionBuilder.DeploymentTarget = AssemblyDeploymentTarget.GlobalAssemblyCache;
            SolutionBuilder.CasPermissions = false;
        }

        public void AddSolutionItems()
        {
            SolutionBuilder.ProjectAssemblyReferences.Add("System.Web.Services");

            // create \ISAPI Folder
            if (!Directory.Exists(Globals.IsapiFolder))
            {
                Directory.CreateDirectory(Globals.IsapiFolder);
            }

            string SolutionSpecificIsapiFolder = Globals.IsapiFolder + @"\" + SolutionBuilder.SolutionName;
            if (!Directory.Exists(SolutionSpecificIsapiFolder))
            {
                Directory.CreateDirectory(SolutionSpecificIsapiFolder);
            }

            string AsmxFilePath = SolutionSpecificIsapiFolder + @"\" + SolutionBuilder.SolutionName + "Service.asmx";
            if (!File.Exists(AsmxFilePath))
            {
                // create config.aspx
                FileStream service_asmx = File.Create(AsmxFilePath);
                StreamWriter service_asmx_writer = new StreamWriter(service_asmx);
                string service_asmx_content = Properties.Resources.Service_asmx.Replace("@SolutionName", SolutionBuilder.SolutionName);
                service_asmx_writer.Write(service_asmx_content.Replace("@FullAssemblyName", SolutionBuilder.FullAssemblyName));
                service_asmx_writer.Dispose();
                service_asmx.Dispose();
                SolutionBuilder.SolutionProvider.ProjectProvider.AddSourceFile(new SourceFile(AsmxFilePath, SourceFileType.None));

                // create code-behind config
                string CodeBehindFilePath = SolutionBuilder.SolutionName + "Service.cs";
                FileStream service_cs = File.Create(CodeBehindFilePath);
                StreamWriter service_cs_writer = new StreamWriter(service_cs);
                service_cs_writer.Write(Properties.Resources.Service_cs.Replace("@SolutionName", SolutionBuilder.SolutionName));
                service_cs_writer.Dispose();
                service_cs.Dispose();
                SolutionBuilder.SolutionProvider.ProjectProvider.AddSourceFile(new SourceFile(CodeBehindFilePath, SourceFileType.Compile));
            }

            string WebConfigFilePath = SolutionSpecificIsapiFolder + @"\web.config";
            if (!File.Exists(WebConfigFilePath))
            {
                // create config.aspx
                FileStream web_config = File.Create(WebConfigFilePath);
                StreamWriter web_config_writer = new StreamWriter(web_config);
                string web_config_content = Properties.Resources.service_web_config;
                web_config_writer.Write(web_config_content);
                web_config_writer.Dispose();
                web_config.Dispose();
                SolutionBuilder.SolutionProvider.ProjectProvider.AddSourceFile(new SourceFile(WebConfigFilePath, SourceFileType.None));
            }
        }


        #region ISolutionProvider Members


        public IProjectFileBuilder ProjectProvider
        {
            get { return _projectFileBuilder; }
        }

        #endregion
    }
}
